package com.applink.sell_proof.utilConvertCount;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.view.View;

import com.applink.sell_proof.pojo.GenericAttribute;
import com.applink.sell_proof.pojo.GenericObject;

public class GenericConverter {
	
	/**
	 * This function is to convert to dataType Integer abc = GenericConverter.convert("5", Integer.class);
	 */
	public static <I, O> O convert(I input, Class<O> outputClass) throws Exception {
        return input == null ? null : outputClass.getConstructor(String.class).newInstance(input.toString());
    }
	
	/**
	 * Convenience method of findViewById of parent, this is used for parent view
	 */
	@SuppressWarnings("unchecked")
	public static <T extends View> T getView(View parent, int id) {
	 return (T) parent.findViewById(id);
	}

	/**
	 * Convenience method of findViewById, this is used for activity
	 */
	@SuppressWarnings("unchecked")
	public static <T extends View> T getView(Activity activity, int id) {
	 return (T) activity.findViewById(id);
	}
	
	/**
	 * This function is to convert from GenericObjectList to hashmap<String,String> of two columns you want
	 */
	public Hashtable<String, String> parseListToHashTable( ArrayList<GenericObject> arrManagerList, 
			String textAttribute, String dataProp) {
		
		Hashtable<String, String> KeyValue = new Hashtable<String, String>();
		KeyValue.clear();
		
		for(int i=0; i< arrManagerList.size();i++){
			//we get the current GenericObject
			//each attribute <name, value> corresspond to each attribute of GenericObject
			@SuppressWarnings("rawtypes")
			ArrayList<GenericAttribute> attributeList = new ArrayList<GenericAttribute> ();
			 attributeList = arrManagerList.get(i).getAttributeList();
				int j=0;
				
				String key ="";
				String value ="";
				
				while (j< attributeList.size()) {
					String attributeName = String.valueOf(attributeList.get(j).getName());
					if(attributeName.compareToIgnoreCase(textAttribute) == 0) {
						key = String.valueOf(attributeList.get(j).getValue());
					}
					
					if(attributeName.compareToIgnoreCase(dataProp) == 0) {
						value = String.valueOf(attributeList.get(j).getValue());
					}
					j++;		
				}
				
				KeyValue.put(key, value);
			}
		
		return KeyValue;
	}
	
}
